var app = new Vue({
    el: '#deliveryer',
    data : {
      user_name:"",//姓名
      phone:"",//电话
      trigger1:"",  //地址
      detailed_add:"", //详细地址
      error_img:true,  //正面
      error_img2:true,  //反面
      popwindow:"asasas",//弹框
      is_show:{display:"none"},//地址显示
      timer:"",//清除定时器
      xuan_is_show:{display:"none"},//选择显示
      province:{data:[]},//省
      city:{data:[]},//市
      area:{data:[]},//区
      street:{data:[]},//街道
      community:{data:[]},//小区
      provinceTip:"请选择",//省tip
      cityTip:"",//市tip
      areaTip:"",//区tip
      streetTip:"",//街道tip
      communityTip:"",//小区tip
      top:{top:"33.35rem"},
      smallCommunityId:"",//地址id用于传参
      url : '',
      tipsText: ''
    },
    methods: {
      //加载图片
      loadImg:function(inputid,imgid,idcard) {
        var animateimg = inputid.value;
        var imgarr = animateimg.split('\\');
        var myimg = imgarr[imgarr.length - 1];
        var suffix = myimg.lastIndexOf('.');//获得后缀
        var ext = myimg.substring(suffix, myimg.length).toUpperCase();//切割后缀，大写
        var file = inputid.files[0];
        if (file !== undefined) {
          if (ext != '.PNG' && ext != '.GIF' && ext != '.JPG' && ext != '.JPEG' && ext != '.BMP') {
            this.poptip('文件类型错误,请上传正确的图片类型');
            return;
          } else {
            idcard == 1 ? this.error_img2 = false : this.error_img = false;
            var reader = new FileReader();
            var imgFile;
            reader.onload = function(e) {
              imgFile = e.target.result;
              imgid.src=imgFile;
            };
            reader.readAsDataURL(file);
          }
        } else {
          this.poptip('请上传身份证正反面照片');
          idcard == 0 ? this.error_img = true : this.error_img2 = true;
        }
      },
      //选地址
      pop_xuan:function(){
        document.activeElement.blur();//清除焦点
        if(this.xuan_is_show.display=="none"){
          this.xuan_is_show.display="block";
          setTimeout(()=>{
            this.top.top="0";
          },100)
          this.provinceTip='请选择';this.cityTip='';this.areaTip='';this.streetTip='';this.communityTip='';
          Axios.post('/area/area/getArea.do', {parent:'1000'}).then(res => {this.province = res}).catch(err => {console.error(err)})
        }else{
          this.top.top="33.35rem";
          setTimeout(()=>{
            this.xuan_is_show.display="none";
          },600)
        }
      },
      //返回选择项
      clear0:function(){this.provinceTip='请选择';this.cityTip='';this.areaTip='';this.streetTip='';this.communityTip=''},
      clear1:function(){this.cityTip='请选择';this.areaTip='';this.streetTip='';this.communityTip=''},
      clear2:function(){this.areaTip='请选择';this.streetTip='';this.communityTip=''},
      clear3:function(){this.streetTip='请选择';this.communityTip=''},
      clear3:function(){this.communityTip='请选择'},
      //请求市
      get_s:function(e){
        this.provinceTip=e.target.innerText;
        this.cityTip="请选择";
        Axios.post('/area/area/getArea.do', {parent:e.target.children[0].innerHTML})
        .then(res => {this.city = res})
        .catch(err => {this.poptip("网络出错")})},
        //请求区
      get_q:function(e){
        this.cityTip=e.target.innerText;
        this.areaTip="请选择";
        Axios.post('/area/area/getArea.do', {parent:e.target.children[0].innerHTML})
        .then(res => {this.area = res})
        .catch(err => {this.poptip("网络出错")})},
        //请求街道
      get_j:function(e){
        this.areaTip=e.target.innerText;
        this.streetTip="请选择";
        Axios.post('/area/area/getArea.do', {parent:e.target.children[0].innerHTML})
        .then(res => {this.street = res;})
        .catch(err => {this.poptip("网络出错")})},
        //请求小区
      get_x:function(e){
        this.streetTip=e.target.innerText;
        this.communityTip="请选择";
        Axios.post('/area/SmallCommunity/getSmallCommunity.do', {areaCode:e.target.children[0].innerHTML})
        .then(res => {this.community = res})
        .catch(err => {this.poptip("网络出错")})},
        //选择完毕
      get_exit:function(e){
        this.communityTip=e.target.innerText;this.trigger1=this.provinceTip+this.cityTip+this.areaTip+this.streetTip+this.communityTip;
        this.smallCommunityId=e.target.children[0].innerHTML;//小区ID
        this.pop_xuan()
      },
      //提示框
      poptip:function(msg,go){
        this.popwindow = msg;
        this.is_show.display = "block";
        this.timer = setTimeout(()=>{
          this.is_show.display = "none";
          if(go)window.location.href = go;
        }, 2000);
      },
      //身份证正面
      idcard_front() {
        let idcard = 0
        this.loadImg(this.$refs.idcard_front,this.$refs.idcardf,idcard)
      },
      //反面
      idcard_back () {
        let idcard = 1
        this.loadImg(this.$refs.idcard_back,this.$refs.idcardb,idcard)
      },
      //获取cookie
      getCookie(name) {
        var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg)) {
            if (name === 'userId') {
                return Number(unescape(arr[2]));
            } else {
                return unescape(arr[2]);
            }
        } else {
            return null;
        }
    },
      submit(){
        this.popwindow = "正在上传图片0% ，请稍等";
        this.is_show.display = "block";
        if (window.location.href.indexOf('laykj.cn') > -1) {
            this.url = 'http://www.laykj.cn/wherebuyAPI/'
        } else if (window.location.href.indexOf('192.168.0.6') > -1) {
            this.url = 'http://192.168.0.6:8080/wherebuyAPI/'
        } else {
            this.url = 'http://192.168.0.100:8080/wherebuyAPI/'
        }
        clearTimeout(this.timer);
        var re = /^[\!\@\#\$\+\`\~\%\^\&\*\(\)\-\[\]\{\}\;\:\'\"\|\<\,\>\.\/\?]+$/i;
        var re1 = /^1[3-9][0-9]\d{8}$/;
        if(this.user_name=="" || this.phone=="" || this.trigger1=="" || this.detailed_add==""){this.poptip("输入不能为空");return;}
        if(this.user_name[0]==' '|| re.test(this.user_name)){this.poptip("非法名字");return;}
        if(!re1.test(this.phone)){this.poptip("手机不正确");return;}
        let address = this.trigger1 + this.detailed_add
        var formdata = new FormData();
        formdata.append("smallCommunityId",parseInt(this.smallCommunityId));
        formdata.append("realName",this.user_name);
        formdata.append("mobile",this.phone);
        formdata.append("address",address);
        formdata.append("file",this.$refs.idcard_front.files[0]);
        formdata.append("file",this.$refs.idcard_back.files[0]);
        var self = this;
        var xhr = new XMLHttpRequest();
        xhr.upload.onprogress = function(e){//加载事件
          self.popwindow = "正在上传图片"+parseInt((e.loaded/e.total)*100)+"% ，请稍等";
          if(e.loaded / e.total==1){
            self.is_show.display = "none";
            xhr.onload = function () {
              if (JSON.parse(xhr.response).respCode === "S") {
                  console.log(1)
                  self.tipsText = '已经提交至系统审核<br>稍后会以短信的方式通知您'
              } else {
                  if (JSON.parse(xhr.response).errorCode === 1906) {
                      window.location.href = '../register/login.html'
                  } else {
                      this.poptip(JSON.parse(xhr.response).errorMsg)
                  }
              }
            }
          };
        }
        xhr.onerror = function () {
          self.poptip('上传失败,可能网络已经掉线');
        }
        xhr.open("post",this.url+'user/authentication/applyDeliverer.do',true);//设置请求参数
        xhr.setRequestHeader('accessToken', this.getCookie('accessToken'));
		    xhr.send(formdata);//发送请求
      },
      btnOk () {
          this.tipsText = ''
          window.location.href = '../my/member_info_authentication.html'
      }
    },
    computed: {}
  })